package com.xzx.study;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.xzx.study.db.DBHelper;

public class SQLiteActivity extends AppCompatActivity implements View.OnClickListener {

    private Button create_db;
    private Button update_db;
    private Button insert;
    private Button update;
    private Button delete;
    private Button query;
    private Button transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        create_db = findViewById(R.id.create_db);
        update_db = findViewById(R.id.update_db);
        insert = findViewById(R.id.insert);
        update = findViewById(R.id.update);
        delete = findViewById(R.id.delete);
        query = findViewById(R.id.query);
        transaction = findViewById(R.id.transaction);

        create_db.setOnClickListener(this);
        update_db.setOnClickListener(this);
        insert.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
        query.setOnClickListener(this);
        transaction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == create_db) {
            DBHelper dbHelper = new DBHelper(this, 1);
            // 不连数据库是不会创建数据库的
            dbHelper.getReadableDatabase();
            Toast.makeText(this, "创建数据库成功", Toast.LENGTH_SHORT).show();
        } else if (v == update_db) {
            DBHelper dbHelper = new DBHelper(this, 2);
            dbHelper.getReadableDatabase();
            Toast.makeText(this, "更新数据库成功", Toast.LENGTH_SHORT).show();
        } else if (v == insert) {
            DBHelper dbHelper = new DBHelper(this, 2);
            SQLiteDatabase database = dbHelper.getReadableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "张三");
            values.put("age", 18);
            database.insert("person", null, values);
            database.close();
            Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
        } else if (v == update) {
            DBHelper dbHelper = new DBHelper(this, 2);
            SQLiteDatabase database = dbHelper.getReadableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "李四");
            values.put("age", 20);
            database.update("person", values,"_id=?", new String[]{"2"});
            database.close();
            Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
        } else if (v == delete) {
            DBHelper dbHelper = new DBHelper(this, 2);
            SQLiteDatabase database = dbHelper.getReadableDatabase();
            database.delete("person", "_id=?", new String[]{"1"});
            database.close();
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        } else if (v == query) {
            DBHelper dbHelper = new DBHelper(this, 2);
            SQLiteDatabase database = dbHelper.getReadableDatabase();
            Cursor cursor = database.query("person", null, null, null, null, null, null);
            StringBuilder builder = new StringBuilder();
            while (cursor.moveToNext()) {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                int age = cursor.getInt(2);
                builder.append(id + ":" + name + ":" + age + "--");
            }
            cursor.close();
            database.close();
            Toast.makeText(this, "查询结果：" + builder.toString(), Toast.LENGTH_SHORT).show();
        } else if (v == transaction) {
            DBHelper dbHelper = new DBHelper(this, 2);
            SQLiteDatabase database = dbHelper.getReadableDatabase();

            database.beginTransaction();
            ContentValues values = new ContentValues();
            values.put("name", "王五");
            values.put("age", 40);
            database.update("person", values,"_id=?", new String[]{"3"});

//            int a = 5 /0;

            values = new ContentValues();
            values.put("name", "孙六");
            values.put("age", 50);
            database.update("person", values,"_id=?", new String[]{"4"});
            database.setTransactionSuccessful();
            database.endTransaction();
            database.close();
            Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
        }
    }
}
